<?php
class SLS_BoUpdateDescription extends SLS_BoControllerProtected 
{	
	public function action()
	{
		$user = $this->hasAuthorative();
		$xml = $this->getXML();
		$xml = $this->makeMenu($xml);
		$pathsHandle = file_get_contents($this->_generic->getPathConfig("configSls")."/backoffice.xml");
		$xmlBo = new SLS_XMLToolbox($pathsHandle);
		
		$table 		= SLS_String::substrAfterFirstDelimiter(SLS_String::trimSlashesFromString($this->_http->getParam("__table")),"_");
		$db			= SLS_String::substrBeforeFirstDelimiter(SLS_String::trimSlashesFromString($this->_http->getParam("__table")),"_");
		$columns 	= $this->_http->getParams();
		$class		= ucfirst($db)."_".SLS_String::tableToClass($table);
		$desc		= SLS_String::trimSlashesFromString($this->_http->getParam("description"));
		
		$this->_generic->useModel($table,$db,"user");
		$object = new $class();
		if (!empty($desc))
			$object->setTableComment($desc,$table,$db);
		
		// Descriptions
		foreach($columns as $key => $value)
		{
			if (SLS_String::startsWith($key,"col_"))
			{
				$column = SLS_String::substrAfterFirstDelimiter($key,"_");
				$object->setColumnComment($column,SLS_String::trimSlashesFromString($value),$table);
			}
		}
				
		// Multilanguages
		$params = array_keys($object->getParams());
		foreach($columns as $key => $value)
		{
			if (SLS_String::startsWith($key,"multi_"))
			{
				$column = SLS_String::substrAfterFirstDelimiter($key,"_");				
				$result = $xmlBo->getTags("//sls_configs/entry[@table='".$db."_".$table."' and @column='".$column."']/@multilanguage");
				unset($params[array_shift(array_keys($params,$column))]);
				
				// If an entry already exists in the XML, delete this record
				if (!empty($result))
				{
					$xmlTmp = $xmlBo->deleteTags("//sls_configs/entry[@table='".$db."_".$table."' and @column='".$column."']");					
					$xmlBo->saveXML($this->_generic->getPathConfig("configSls")."/backoffice.xml",$xmlTmp);
					$pathsHandle = file_get_contents($this->_generic->getPathConfig("configSls")."/backoffice.xml");
					$xmlBo = new SLS_XMLToolbox($pathsHandle);
				}
				
				$xmlNode = '<entry table="'.$db.'_'.$table.'" column="'.$column.'" multilanguage="true"/>';				
				$xmlBo->appendXMLNode("//sls_configs",$xmlNode); 
				$xmlBo->saveXML($this->_generic->getPathConfig("configSls")."/backoffice.xml",$xmlBo->getXML());
			}
		}
		foreach($params as $param)
		{
			$result = $xmlBo->getTags("//sls_configs/entry[@table='".$db."_".$table."' and @column='".$param."']/@multilanguage");			
			if (!empty($result))
			{
				$xmlTmp = $xmlBo->deleteTags("//sls_configs/entry[@table='".$db."_".$table."' and @column='".$param."']");					
				$xmlBo->saveXML($this->_generic->getPathConfig("configSls")."/backoffice.xml",$xmlTmp);
				$pathsHandle = file_get_contents($this->_generic->getPathConfig("configSls")."/backoffice.xml");
				$xmlBo = new SLS_XMLToolbox($pathsHandle);
			}
			
			$xmlNode = '<entry table="'.$db.'_'.$table.'" column="'.$param.'" multilanguage="false"/>';				
			$xmlBo->appendXMLNode("//sls_configs",$xmlNode); 
			$xmlBo->saveXML($this->_generic->getPathConfig("configSls")."/backoffice.xml",$xmlBo->getXML());
		}
		
		$controllers = $this->_generic->getTranslatedController("SLS_Bo","EditModel");
		$this->_generic->redirect($controllers["controller"]."/".$controllers["scontroller"]."/name/".$db."_".$table);
		
		$this->saveXML($xml);
	}
	
}
?>